﻿
Partial Class Apps_Shares_Uc_type_fv
    Inherits System.Web.UI.UserControl

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            If Not IsPostBack Then
                InitCtrl()
            End If
        Catch ex As Exception
            GenericHelper.ShowAlert(Me, ex.Message)
        End Try
    End Sub

    Sub InitCtrl()
        Dim BreadCrumpBG As HtmlControl = Page.Master.FindControl("tdBreadCrump")
        Dim BreadCrump As Literal = Page.Master.FindControl("ltrBreadCrump")
        'Change mode by querystring
        If Not String.IsNullOrEmpty(Request.QueryString("ReadOnly")) Then
            CodeMatrix.Business.GenericData.SetBreadCrump(BreadCrumpBG, BreadCrump, 17)
            fv.ChangeMode(FormViewMode.ReadOnly)
        ElseIf Not String.IsNullOrEmpty(Request.QueryString("Insert")) Then
            CodeMatrix.Business.GenericData.SetBreadCrump(BreadCrumpBG, BreadCrump, 18)
            fv.ChangeMode(FormViewMode.Insert)
        ElseIf Not String.IsNullOrEmpty(Request.QueryString("Edit")) Or String.IsNullOrEmpty(Request.QueryString("Update")) Then
            CodeMatrix.Business.GenericData.SetBreadCrump(BreadCrumpBG, BreadCrump, 19)
            fv.ChangeMode(FormViewMode.Edit)
        End If
    End Sub

    Protected Sub fv_ItemInserted(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.FormViewInsertedEventArgs) Handles fv.ItemInserted
        Try
            e.ExceptionHandled = True
            If e.Exception Is Nothing Then
                ' log insert
                GenericHelper.GenerateScript(GenerateScriptLogType.Insert, LogMenuType.master_type, e.Values)
                GenericHelper.ShowAlertSuccessful(Me)
            Else
                Throw New Exception(e.Exception.Message)
            End If
        Catch ex As Exception
            e.KeepInInsertMode = True
            GenericHelper.ShowAlert(Me, ex.Message)
        End Try
    End Sub

    Protected Sub fv_ItemInserting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.FormViewInsertEventArgs) Handles fv.ItemInserting
        Try
            ' Server validation ---------------------------
            If e.Values("type_name").ToString().Trim() = "" Then
                Throw New Exception("กรุณากรอกชือประเภทของการจัดการข้อมูล")
            End If
            ' Server validation ---------------------------
        Catch ex As Exception
            e.Cancel = True
            GenericHelper.ShowAlert(Me, ex.Message)
        End Try
    End Sub

    Protected Sub fv_ItemUpdated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.FormViewUpdatedEventArgs) Handles fv.ItemUpdated
        Try
            e.ExceptionHandled = True
            If e.Exception Is Nothing Then
                ' log insert
                GenericHelper.GenerateScript(GenerateScriptLogType.Update, LogMenuType.master_type, e.NewValues, e.OldValues)
                'Redirect to list page
                Response.Redirect("type_list.aspx?Edit=true")
            Else
                Throw New Exception(e.Exception.Message)
            End If
        Catch ex As Exception
            e.KeepInEditMode = True
            GenericHelper.ShowAlert(Me, ex.Message)
        End Try
    End Sub

    Protected Sub fv_ItemUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.FormViewUpdateEventArgs) Handles fv.ItemUpdating
        Try
            ' Server validation ---------------------------
            If e.NewValues("type_name").ToString().Trim() = "" Then
                Throw New Exception("กรุณากรอกชือประเภทของการจัดการข้อมูล")
            End If
            ' Server validation ---------------------------
        Catch ex As Exception
            e.Cancel = True
            GenericHelper.ShowAlert(Me, ex.Message)
        End Try
    End Sub
End Class
